﻿//----------------------------------------------------------------------------------------------------
// The Camera Manager deals with controlling the players view
//----------------------------------------------------------------------------------------------------
package HG.Managers
{
	import HG.GameService;
	import HG.Debug;
	
	import HG.Visual.VisualCameraCanvas;
	import HG.Visual.VisualNode;
	import HG.Visual.VisualAttachPoint;
	
	import HG.Events.GameEvent;
	import HG.GameDataTypes.Player;
	
	public class CameraManager extends IGameManager
	{
		var m_levelManager:LevelManager;
		var m_gameRuleManager:GameRuleManager;
		var m_playerManager:PlayerManager;
		
		var m_canvas:VisualCameraCanvas;
		
		static var SCREEN_WIDTH:Number = 1280;
		static var SCREEN_HEIGHT:Number = 720;
		
		//----------------------------------------------------------------------------------------------------
		public override function CameraManager()
		{
			
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function Initialize()
		{
			m_levelManager = GameService.GetInstance().GetLevelManager();
			m_gameRuleManager = GameService.GetInstance().GetGameRuleManager();
			m_playerManager = GameService.GetInstance().GetPlayerManager();
			
			m_gameRuleManager.addEventListener(GameEvent.BEGIN, OnGameStart);
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function Shutdown()
		{
			
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function OnTick(DeltaTime:Number)
		{
			
		}
		
		//----------------------------------------------------------------------------------------------------
		private function OnGameStart(evt:GameEvent)
		{
			var currentPlayerID:Number = m_playerManager.GetCurrentPlayerID();
			var activePlayer:Player = m_playerManager.GetPlayer(currentPlayerID);
			var rootNode:Number = activePlayer.GetRootNode();
			
			LookAtNode(rootNode);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetCanvas(canvas:VisualCameraCanvas)
		{
			m_canvas = canvas;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function LockCamera(locked:Boolean)
		{
			if (!m_canvas)
				return;
				
			m_canvas.LockPosition(locked);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function LookAtNode(nodeID:Number)
		{
			var visNode:VisualNode = m_levelManager.GetVisualNode(nodeID);
			
			if (!visNode)
				return;
			
			LookAt(visNode.x, visNode.y);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function LookAt(x:Number, y:Number)
		{
			if (!m_canvas)
				return;
				
			if (x > m_canvas.width || 
				x < 0 ||
				y > m_canvas.height ||
				y < 0)
				return;
				
			var centerX:Number = SCREEN_WIDTH / 2;
			var centerY:Number = SCREEN_HEIGHT / 2;
			
			var xPos:Number = (-x) + centerX;
			var yPos:Number = (-y) + centerY;
			
			m_canvas.SetPosition( xPos, yPos);
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}